package com.helloandroid.activitys

import android.widget.*
import androidx.activity.viewModels
import com.business.Analyse
import com.business.AnalyseKey
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.R
import com.helloandroid.ads.VideoAdUtil
import com.helloandroid.base.BaseActivity
import com.helloandroid.dialogs.AwardDialog
import com.helloandroid.models.butie.ZaoWanViewModel
import com.helloandroid.tools.MyLog
import com.helloandroid.tools.MyTimeUtils
import com.helloandroid.tools.NetWorkUtil
import com.helloandroid.vo.TimeType
import com.helloandroid.vo.ZWData
import com.helloandroid.vo.ZaoWanState
import java.util.*

data class DataTemplate(
        val type: TimeType,  // 早 还是 晚
        val start: Int, // 打卡开始时间
        val end: Int,  // 打卡结束时间
        val contentRes: Int,  // 内容图片资源
        val bgRes: Int,  // 背景资源
        val desc: String,  // 文本描述
        val toggleBtnRes: Int, // 切换按钮资源
        var isExecuted: Boolean = false
)

// 早上打卡数据
private val dataZaoShang = DataTemplate(
        TimeType.ZAO,
        5,
        9,
        R.drawable.dd_zwdk_baitian,
        R.drawable.dd_zwdk_baitian_bg,
        "早起锻炼精神好~",
        R.drawable.dd_zwdk_toggle_on)

// 晚上打卡数据
private val dataWanShang = DataTemplate(
        TimeType.WAN,
        18,
        23,
        R.drawable.dd_zwdk_yewan,
        R.drawable.dd_zwdk_yewan_bg,
        "再忙也要记得早睡哦~",
        R.drawable.dd_zwdk_toggle_off
)


class ZaoWanDaKaActivity : BaseActivity() {

    val model: ZaoWanViewModel by viewModels()

    // views
    private lateinit var tvWelcome: TextView
    private lateinit var tvDayTime: TextView
    private lateinit var ibToggle: ImageButton
    private lateinit var tvDesc: TextView
    private lateinit var ivContent: ImageView
    private lateinit var svBg: ScrollView
    private lateinit var tvTimeDesc: TextView
    private lateinit var btnDaka: Button
//    private lateinit var rlDouble: TextView

    private var templateData: DataTemplate? = null
    private val zwData = ZWData()
    private val calendar = Calendar.getInstance()

    //
    private var isReady = false

    override fun getContentViewResId() = R.layout.activity_zaowandaka_new

    override fun initView() {

        // init views
        ibToggle = findViewById(R.id.ibToggle)
        tvWelcome = findViewById(R.id.tvWelcome)
        tvDayTime = findViewById(R.id.tvDayTime)
        tvDesc = findViewById(R.id.tvDesc)
        ivContent = findViewById(R.id.ivContent)
        svBg = findViewById(R.id.svBg)
        tvTimeDesc = findViewById(R.id.tvTimeDesc)
        btnDaka = findViewById(R.id.btnDaka)
//        rlDouble = findViewById(R.id.rlDouble)

        // btns
        // back
        findBtn(R.id.ibBack).setOnClickListener { finish() }

        // 切换早晚
        findBtn(R.id.ibToggle).setOnClickListener { toggleBtnClicked() }

        // 打卡
        btnDaka.setOnClickListener { dakaBtnClicked() }
    }

    private fun daka(zwData:ZWData,video:Boolean){
        if (!NetWorkUtil.networkConnected()){
            AppUtil.toast("请打开网络!")
            return
        }
        val awardCoin = 58
        AppUtil.appViewModel().zaoWanDaKa(awardCoin,zwData.time)

        // 直接更新按钮为已打卡
        val reason:String
        if (zwData.time == TimeType.ZAO){
            dataZaoShang.isExecuted = true
            templateData = dataZaoShang

            if (video){
                Analyse.report(AnalyseKey.ZaoWan_ZaoDaKa_Video)
            }else{
                Analyse.report(AnalyseKey.ZaoWan_ZaoDaKa)
            }
            reason = AnalyseKey.ZaoWan_ZaoDaKa_Double
        }else{
            dataWanShang.isExecuted = true
            templateData = dataWanShang

            if (video){
                Analyse.report(AnalyseKey.ZaoWan_WanDaKa_Video)
            }else{
                Analyse.report(AnalyseKey.ZaoWan_WanDaKa)
            }
            reason = AnalyseKey.ZaoWan_WanDaKa_Double
        }

        // 修改按钮状态
        calendar.timeInMillis = AppUtil.sysTime
        updateDakaBtn(templateData!!,calendar.get(Calendar.HOUR_OF_DAY))

        val dialog = AwardDialog(this,awardCoin)
        dialog.reasonKey = reason
        dialog.show()
    }

    override fun initData() {
        model.getInfo { success,zaoWan ->
            isReady = success
            if (isReady){
                dataZaoShang.isExecuted = zaoWan!!.zaoExecuted > 0
                dataWanShang.isExecuted = zaoWan.wanExecuted > 0
                initUI()
            }
        }
    }

    private fun initUI() {
        // welcome
        tvWelcome.text = AppUtil.getWelComeName()

        // 显示时间
        tvDayTime.text = MyTimeUtils.dayTimeString(AppUtil.sysTime)

        // 判断现在的时间
        calendar.timeInMillis = AppUtil.sysTime
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        templateData = if (hour >= 12) {
            dataWanShang
        } else {
            dataZaoShang
        }
        fillUI(templateData!!, hour)
    }

    private fun fillUI(data: DataTemplate, hour: Int) {
        ibToggle.setBackgroundResource(data.toggleBtnRes)
        tvDesc.text = data.desc
        ivContent.setImageResource(data.contentRes)
        svBg.setBackgroundResource(data.bgRes)
        if (data.type == TimeType.WAN) {
            zwData.time = TimeType.WAN
            tvTimeDesc.text = String.format("晚上打卡 %d:00-%d:00", data.start, data.end)
        } else {
            zwData.time = TimeType.ZAO
            tvTimeDesc.text = String.format("早上打卡 0%d:00-0%d:00", data.start, data.end)
        }
        updateDakaBtn(data, hour)
    }

    // 更新按钮状态
    private fun updateDakaBtn(data: DataTemplate, hour: Int) {
        MyLog.elog("updateDakaBtn:$hour")
        if (data.isExecuted) {
            // 已经打卡
            btnDaka.setBackgroundResource(R.drawable.dd_btn_gray)
            btnDaka.text = "已打卡"
            btnDaka.isClickable = false
//            rlDouble.visibility = View.INVISIBLE
            zwData.state = ZaoWanState.AlreadyDaKa
        } else {
            // 没有打卡.
            // 1. 时间没有到
            // 2. 时间到了,但是还没打
            // 3. 时间过了,补卡
            if (hour < data.start) {
                // case 1
                btnDaka.setBackgroundResource(R.drawable.dd_btn_gray)
                btnDaka.text = "时间还未到"
                btnDaka.isClickable = false
//                rlDouble.visibility = View.INVISIBLE
                zwData.state = ZaoWanState.TimeNotYet
            } else if (hour >= data.end) {
                // case 3
                btnDaka.setBackgroundResource(R.drawable.btn_red)
                btnDaka.text = "补卡"
                btnDaka.isClickable = true
//                rlDouble.visibility = View.INVISIBLE
                zwData.state = ZaoWanState.BuKa
            } else {
                // 可以打卡
                btnDaka.setBackgroundResource(R.drawable.btn_blue)
                btnDaka.text = "打卡"
                btnDaka.isClickable = true
//                rlDouble.visibility = View.VISIBLE
                zwData.state = ZaoWanState.NormalDaKa
            }
        }
    }

    override fun isDarkMode() = false

    private fun toggleBtnClicked(){
        if (!isReady) {
            return
        }

        templateData = if (templateData!!.type == TimeType.ZAO) {
            dataWanShang
        } else {
            dataZaoShang
        }

        calendar.timeInMillis = AppUtil.sysTime
        fillUI(templateData!!, calendar.get(Calendar.HOUR_OF_DAY))
    }
    private fun dakaBtnClicked(){
        if (!isReady) {
            return
        }
        MyLog.elog("state:${zwData.state},time:${zwData.time}")
        if (zwData.state == ZaoWanState.AlreadyDaKa ||
                zwData.state == ZaoWanState.TimeNotYet) {
            return
        }

        if (zwData.state == ZaoWanState.NormalDaKa) {
            daka(zwData,false)
        } else if (zwData.state == ZaoWanState.BuKa) {
            // 需要看视频
            VideoAdUtil.playVideoAd(this){success->
                if (success){
                    daka(zwData,true)
                }else{
                    AppUtil.toast("视频还没准备好!")
                }
            }
        }
    }
}